import axios from "axios";
import config from "../config/configIndex.js";
import mock from "mockjs";
/**
 * 请求拦截器
 */
axios.interceptors.request.use(
    function(config) {
        // Do something before request is sent
        return config;
    },
    function(error) {
        return Promise.reject(error);
    }
);

/**
 * 响应拦截器
 */
axios.interceptors.response.use(
    function(response) {
        return response;
    },
    function(error) {
        return Promise.reject(error);
    }
);
/**
 * 检查响应结果状态
 * @param response
 * @returns {*}
 */
function checkStatus(response) {
    if (response.status === 200 || response.status === 304) {
        return response;
    }
    return {
        data: {
            code: -404,
            message: response.statusText,
            data: response.statusText
        }
    };
}
/**
 * 检查结果状态
 * @param res
 * @returns {*}
 */
function checkCode(res) {
    return res;
}
/**
 * 提交新增数据
 * @param url
 * @param data
 * @returns {Promise.<TResult>|*}
 */
function postData(url, data,cusHeader={}) {
    let isMock = config.baseSetting.isMock=="true"?true:false;
    let template = data.template || "";
    if (isMock && template != null && template != undefined) {
        mock.mock(url, template);
    } else {
        data.template = "";
    }
    console.info("cisdi post url==========" + url);
    delete data.template;
    console.info("cisdi post params==========" + JSON.stringify(data));
    let formalHeader={
            "Access-Control-Allow-Origin": "*",
            "X-Requested-With": "XMLHttpRequest",
            "Content-Type": "application/Json; charset=UTF-8",
     };
    return axios({
            method: "post",
            url: url,
            data: JSON.stringify(data),
            params: [],
            timeout: 30000,
            headers: buildCusHeader(formalHeader,cusHeader)
        })
        .then(checkStatus)
        .then(checkCode);
}
/**
 * 提交更新数据
 * @param url
 * @param data
 * @returns {Promise.<TResult>|*}
 */
function putData(url, data,cusHeader={}) {
    let isMock =config.baseSetting.isMock=="true"?true:false;
    let template = data.template || "";
    if (isMock && template != null && template != undefined) {
        mock.mock(url, template);
    } else {
        data.template = "";
    }
    console.info("cisdi post url==========" + url);
    delete data.template;
    console.info("cisdi post params==========" + JSON.stringify(data));
    let formalHeader={
        "Access-Control-Allow-Origin": "*",
        "X-Requested-With": "XMLHttpRequest",
        "Content-Type": "application/json",
    };
    return axios({
            method: "put",
            url: url,
            data: JSON.stringify(data),
            params: [],
            timeout: 30000,
            headers: buildCusHeader(formalHeader,cusHeader)
        })
        .then(checkStatus)
        .then(checkCode);
}

/**
 * 提交删除数据
 * @param url
 * @param data
 * @returns {Promise.<TResult>|*}
 */
function deleteData(url, data,cusHeader={}) {
    let isMock =config.baseSetting.isMock=="true"?true:false;
    let template = data.template || "";
    if (isMock && template != null && template != undefined) {
        mock.mock(url, template);
    } else {
        data.template = "";
    }
    console.info("cisdi post url==========" + url);
    delete data.template;
    console.info("cisdi post params==========" + JSON.stringify(data));
    let formalHeader={
        "Access-Control-Allow-Origin": "*",
        "X-Requested-With": "XMLHttpRequest",
        "Content-Type": "application/json",
        access_token: session.getAccessToken()
    };
    return axios({
            method: "delete",
            url: url,
            data: JSON.stringify(data),
            params: [],
            timeout: 30000,
            headers:buildCusHeader(formalHeader,cusHeader)
        })
        .then(checkStatus)
        .then(checkCode);
}

/**
 * 获取数据
 * @param url
 * @param params
 * @returns {Promise.<TResult>|*}
 */
function getData(url,data,cusHeader={}) {
    let isMock = config.baseSetting.isMock=="true"?true:false;
    let template = data != null && data.template != null ? data.template :null;
    let params = new Object();
    if (isMock && template != null && template != undefined) {
        mock.mock(url, template);
        params = [];
    } else {
        data.template = "";
        params = data;
    }
    console.info("cisdi post url==========" + url);
    delete data.template;
    console.info("cisdi post params==========" + JSON.stringify(data));
    let formalHeader={
        "Access-Control-Allow-Origin": "*",
        "X-Requested-With": "XMLHttpRequest",
    };
    return axios({
            method: "get",
            url: url,
            data: [],
            params: params,
            timeout: 30000,
            dataType: "json",
            headers:buildCusHeader(formalHeader,cusHeader)
        })
        .then(checkStatus)
        .then(checkCode);
}

/**
 * 构造自定义请求头
 * @param cusHeader
 */
function buildCusHeader(formalHeader={},cusHeader={}) {
    let header={};
    Object.assign(header,formalHeader,cusHeader);
    return header;
}
export default {
    getData,
    putData,
    postData,
    deleteData,
};
